﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using CodeWork.Library;
using System.Data.SqlTypes;
using System.Drawing;

public partial class controls_DatePicker : System.Web.UI.UserControl
{
    private string _validationGroup;
    public string ValidationGroup
    {
        get { return this._validationGroup; }
        set { this._validationGroup = value; }
    }

    private string _dropDownCssClass;
    public string DropDownCssClass
    {
        get { return this._dropDownCssClass; }
        set { this._dropDownCssClass = value; }
    }

    private string _textBoxCssClass;
    public string TextBoxCssClass
    {
        get { return this._textBoxCssClass; }
        set { this._textBoxCssClass = value; }
    }

    private string _replaceFontColor;
    public string ReplaceFontColor
    {
        get { return this._replaceFontColor; }
        set { this._replaceFontColor = value; }
    }

    private string _text;
    public string Text
    {
        get { return this._text; }
        set { this._text = value; }
    }

    public DateTime Date
    {
        get
        {
            int day = 0;
            int year = 0;
            int month = 0;
            if (!int.TryParse(tbxDay.Text, out day) || !int.TryParse(tbxYear.Text, out year) ||
                !int.TryParse(uccMonth.SelectedValue, out month))
            {
                return SqlDateTime.MinValue.Value;
            }

            if(year<1753 || year>9999 || day<1 || day>31 || month<1 || month>12)
                return SqlDateTime.MinValue.Value;

            DateTime date = new DateTime(year, month, day);
            return date;     
        }
        set
        {
            DateTime date = value;
            if (date.Year < 1753 || date.Year > 9999 || date.Day < 1 || date.Day > 31 || date.Month < 1 || date.Month > 12)
            {
                uccMonth.SelectedValue = "1";
                tbxYear.Text = "1753";
                tbxDay.Text = "1";
            }
            else
            {
                uccMonth.SelectedValue = date.Month.ToString();
                tbxYear.Text = date.Year.ToString();
                tbxDay.Text = date.Day.ToString();
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            uccMonth.DataBind();
            tbxDay.ForeColor = Color.FromArgb(161, 161, 161);
            tbxYear.ForeColor = Color.FromArgb(161, 161, 161);

            if (!string.IsNullOrEmpty(this._text))
                cuvDateValidator.Text = this._text;
        }
        else
        {
            System.Drawing.Color dimColor = System.Drawing.Color.FromArgb(161, 161, 161);
            System.Drawing.Color trueColor = System.Drawing.ColorTranslator.FromHtml("#575757");

            if (tbxDay.Text == "Day")
                tbxDay.ForeColor = dimColor;
            else
                tbxDay.ForeColor = trueColor;

            if (tbxYear.Text == "Year")
                tbxYear.ForeColor = dimColor;
            else
                tbxYear.ForeColor = trueColor;
        }
    
        if (!string.IsNullOrEmpty(this._validationGroup))
        {
            cuvDateValidator.ValidationGroup = this._validationGroup;
        }

        if (!string.IsNullOrEmpty(this._textBoxCssClass))
        {
            tbxDay.CssClass = this._textBoxCssClass;
            tbxYear.CssClass = this._textBoxCssClass;
        }

        if (!string.IsNullOrEmpty(this._dropDownCssClass))
        {
            uccMonth.CssClass = this._dropDownCssClass;
        }

        tbxDay.Attributes.Add("onclick","javascript:clearText(this,'Day','" + ReplaceFontColor +"')");
        tbxDay.Attributes.Add("onkeyup", "javascript:clearText(this,'Day','" + ReplaceFontColor + "')");
        tbxDay.Attributes.Add("onblur", "javascript:resetText(this,'Day','" + ReplaceFontColor + "')");
        tbxYear.Attributes.Add("onkeyup","javascript:clearText(this,'Year','"+ ReplaceFontColor +"')");
        tbxYear.Attributes.Add("onclick", "javascript:clearText(this,'Year','" + ReplaceFontColor + "')");
        tbxYear.Attributes.Add("onblur", "javascript:resetText(this,'Year','" + ReplaceFontColor + "')");

    }

    protected void ValidateDate(object sedner, ServerValidateEventArgs e)
    {
        e.IsValid = false;
        try
        {
            //                        Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
            int[] daysOfMonths = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

            int day = 0;
            int year = 0;
            int month = 0;
            if (!int.TryParse(tbxDay.Text, out day))
            {
                e.IsValid = false;
                return;
            }
            if (!int.TryParse(tbxYear.Text, out year))
            {
                e.IsValid = false;
                return;
            }
            if (!int.TryParse(uccMonth.SelectedValue, out month))
            {
                e.IsValid = false;
                return;
            }
            if (month < 1 || month > 12)
            {
                e.IsValid = false;
                return;
            }
            if (day < 1 || day > 31)
            {
                e.IsValid = false;
                return;
            }
            if (year < 1753 || year > 9999)
            {
                e.IsValid = false;
                return;
            }

            if (month == 2)
            {
                if (DateTime.IsLeapYear(year))
                {
                    // if the year is leap year and the month 
                    // is february day should be withing 29
                    if (day > 29)
                        e.IsValid = false;
                    else
                        e.IsValid = true;
                }
                else
                {
                    if (day > daysOfMonths[month])
                        e.IsValid = false;
                    else
                        e.IsValid = true;
                }
            }
            else
            {
                if (day > daysOfMonths[month])
                    e.IsValid = false;
                else
                    e.IsValid = true;
            }
        }
        catch
        {
            e.IsValid = false;
        }
    }
}
